package com.ruoyi.web.controller.regulatory;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.URLUtil;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.regulatory.domain.JyBureau;
import com.ruoyi.regulatory.domain.JySchool;
import com.ruoyi.regulatory.service.JyBureauService;
import com.ruoyi.regulatory.service.JySchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 学校基础信息Controller
 *
 * @author aulbrother
 * @date 2024-04-06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/regulatory/school")
public class JySchoolController extends BaseController {
    private final JySchoolService jySchoolService;
    private final JyBureauService jyBureauService;

    /**
     * 查询学校基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('regulatory:school:list')")
    @GetMapping("/list")
    public TableDataInfo list(JySchool jySchool) {
        startPage();
        List<JySchool> list = jySchoolService.selectJySchoolList(jySchool);

        if (CollectionUtil.isNotEmpty(list)) {
            Map<Long, JyBureau> longJyBureauMap = jyBureauService.selectJyBureauMap();

            list.forEach(v -> {
                Long bureauId = v.getBureauId();
                if (null != bureauId) {
                    JyBureau jyBureau = longJyBureauMap.get(bureauId);
                    if (null != jyBureau) {
                        v.setBureauName(jyBureau.getName());
                    }
                }
            });
        }

        return getDataTable(list);
    }

    /**
     * 导出学校基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('regulatory:school:export')")
    @Log(title = "学校基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JySchool jySchool) throws IOException {
        List<JySchool> list = jySchoolService.selectJySchoolList(jySchool);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "学校基础信息数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), JySchool.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取学校基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('regulatory:school:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jySchoolService.selectJySchoolById(id));
    }

    /**
     * 新增学校基础信息
     */
    @PreAuthorize("@ss.hasPermi('regulatory:school:add')")
    @Log(title = "学校基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JySchool jySchool) {
        return toAjax(jySchoolService.insertJySchool(jySchool));
    }

    /**
     * 修改学校基础信息
     */
    @PreAuthorize("@ss.hasPermi('regulatory:school:edit')")
    @Log(title = "学校基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JySchool jySchool) {
        return toAjax(jySchoolService.updateJySchool(jySchool));
    }

    /**
     * 删除学校基础信息
     */
    @PreAuthorize("@ss.hasPermi('regulatory:school:remove')")
    @Log(title = "学校基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jySchoolService.deleteJySchoolByIds(ids));
    }
}
